/*
 * ServerAlive.cpp
 *
 *  Created on: 08.12.2011
 *      Author: Pavel Sinay
 */

#include "ServerAlive.h"
#include "../NetException.h"
#include <iostream>

namespace net {

ServerAlive::ServerAlive() :
	m_client_socket(NULL) {
}

ServerAlive::~ServerAlive() {
}

bool ServerAlive::isClientPresented() {
	if (m_client_socket) {
		return m_client_socket->isConnected();
	} else {
		return false;
	}
}

void ServerAlive::onClientAccepted(Socket *client_socket) {
	m_client_socket = client_socket;
	std::cerr << "ServerAlive::onClientAccepted(): new client" << std::endl;
}

std::vector<char> ServerAlive::send(std::vector<char> tx_message) {
	if (!m_client_socket || !m_client_socket->isConnected()) {
		throw NetException("Client is not connected");
	} else {
		std::vector<char> rx_message;
		m_client_socket->sendMessage(tx_message);
		m_client_socket->receiveMessage(rx_message);
		return rx_message;
	}
}

}
